//
//  WQVisitorView.swift
//  sanwuWeibo
//
//  Created by 王先强 on 15/12/6.
//  Copyright © 2015年 王先强. All rights reserved.
//

import UIKit

protocol WQVisitorViewDelegate : NSObjectProtocol {
    func visitorViewDidRediser()
    func visitorViewDidLogin()
}


class WQVisitorView: UIView {
    
    
    weak var delegate: WQVisitorViewDelegate?
    @objc private func loginClick() {
        delegate?.visitorViewDidLogin()
    }
    @objc private func regiserClick() {
        delegate?.visitorViewDidRediser()
    }
    
    ///  设置是否为主页的控制器
    func setupVisitorViewInfo(image: String?, infoString: String) {
        
        homeLable.text = infoString
        guard let imageName = image else {
            circleViewAnimation()
            return
        }
        circleView.hidden = true
        homeView.image = UIImage(named: imageName)
    }
    
    
    // MARK: - 代码创建的view的时候调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupSubViews()
    }
    // MARK: - storyboard 或者Xib 创建的view的时候调用
    required init?(coder aDecoder: NSCoder) {
        // 如果使用用代码创建view再用 storyboard 开发，调用这个视图，会直接崩溃
        // fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupSubViews()
    }
    //主页后面的圆环
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var maskCircleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //主页的图片
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //主页上的文字label
    private lazy var homeLable: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
    
    //主页上的注册按钮
    private lazy var registerButton: UIButton = UIButton(backgroundImageName: "common_button_white_disable", title: "注册", color: UIColor.blueColor())
    
    
    //主页上的登陆按钮
    private lazy var loginButton: UIButton = UIButton(backgroundImageName: "common_button_white_disable", title: "登陆", color: UIColor.darkGrayColor())
    
    
    private func circleViewAnimation() {
        let ani = CABasicAnimation()
        ani.keyPath = "transform.rotation"
        ani.toValue = M_PI * 2
        ani.duration = 20
        ani.repeatCount = MAXFLOAT
        ani.removedOnCompletion = false
        circleView.layer.addAnimation(ani, forKey: nil)
    }
    
}

extension WQVisitorView {
    
    ///  设置界面
    private func  setupSubViews()
    {
        // 1.添加子控件
        addSubview(circleView)
        addSubview(maskCircleView)
        addSubview(homeView)
        addSubview(homeLable)
        addSubview(registerButton)
        addSubview(loginButton)
   
        //遍历子控件，移除Autoresizing
        for subview in subviews {
            subview.translatesAutoresizingMaskIntoConstraints = false
        }
        
        /// 2.布局子控件
        
        //circleView布局
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        //homeView布局
        addConstraint(NSLayoutConstraint(item: homeView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //homeLable布局
        addConstraint(NSLayoutConstraint(item: homeLable, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeLable, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 30))
        addConstraint(NSLayoutConstraint(item: homeLable, attribute: .LeftMargin, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1.0, constant: 80))
        addConstraint(NSLayoutConstraint(item: homeLable, attribute: .RightMargin, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1.0, constant: -80))
        //registerButton布局
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: -60))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        //loginButton布局
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant:  60))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //遮挡罩maskCircleView布局
        /**
        VFL : 可视化格式语言
        
        H 水平方向
        V 垂直方向
        | 边界
        [] 包装控件
        views: 是一个字典 [名字: 控件名] - VFL 字符串中表示控件的字符串
        metrics: 是一个字典 [名字: NSNumber] - VFL 字符串中表示某一个数值
        */
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask":maskCircleView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeigth)-[btn]", options: [], metrics: ["btnHeigth": -35], views: ["mask": maskCircleView, "btn": registerButton]))
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
        registerButton.addTarget(self, action: "regiserClick", forControlEvents: UIControlEvents.TouchUpInside)
        loginButton.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
    }

    
    
    
}